package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.BasePageInfo;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.bean.MarketCoupon;
import com.cskaoyan.bean.param.CommonData;
import com.cskaoyan.bean.vo.coupon.CouponUserVO;
import com.cskaoyan.service.admin.MarketCouponService;
import com.cskaoyan.service.wx.WXCouponService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author RXC
 * @date 2022/6/29 10:58
 */
@RestController
@RequestMapping("wx/coupon")
public class WXCouponController {

    // 复用后台的service
    @Autowired
    MarketCouponService marketCouponService;

    // 自己的Service
    @Autowired
    WXCouponService wxCouponService;

    /**
     * 进入优惠券专区，分页显示所有优惠券，对应后台list
     * ?page=1&limit=10, 不需要手动添加sort=add_time
     * @param basePageInfo
     * @return CommonData
     */
    @RequestMapping("list")
    public BaseRespVo list(BasePageInfo basePageInfo) {
        // basePageInfo.setSort("add_time");
        CommonData<MarketCoupon> data = marketCouponService.list(null, null, (short) 0, basePageInfo);
        return BaseRespVo.ok(data);
    }

    /**
     * 首页收取优惠券
     * @param {couponId: 8}
     * @return {"errno":0,"errmsg":"成功"}，{"errno":740,"errmsg":"优惠券已经领取过"}
     * 可能要考虑其他情况
     */
    @RequestMapping("receive")
    public BaseRespVo receive(@RequestBody Map<String, Integer> map) {
        // 判断登录
        BaseRespVo loginBaseRespVo = getLoginBaseRespVo();
        if (loginBaseRespVo != null) return loginBaseRespVo;

        Integer couponId = map.get("couponId");
        int code = wxCouponService.receive(couponId);
        // 根据返回状态码判断结果
        BaseRespVo couponBaseRespVo = getCouponBaseRespVo(code, 740);
        if (couponBaseRespVo != null) return couponBaseRespVo;
        return BaseRespVo.ok();
    }

    private BaseRespVo getLoginBaseRespVo() {
        // 判断登录
        Subject subject = SecurityUtils.getSubject();
        PrincipalCollection principals = subject.getPrincipals();
        if (ObjectUtils.isEmpty(principals)) {
            return BaseRespVo.error(740, "未登录");
        }
        return null;
    }

    /**
     * 个人主页优惠券选项兑换优惠券
     * @Param {code: "someCode"}
     * @return {"errno":0,"errmsg":"成功"}，{"errno":742,"errmsg":"优惠券不正确"}
     */
    @RequestMapping("exchange")
    public BaseRespVo exchange(@RequestBody Map<String, String> map) {
        // 判断登录
        BaseRespVo loginBaseRespVo = getLoginBaseRespVo();
        if (loginBaseRespVo != null) return loginBaseRespVo;

        String code = map.get("code");
        int resultCode = wxCouponService.exchange(code);
        // 根据返回状态码判断结果
        BaseRespVo couponBaseRespVo = getCouponBaseRespVo(resultCode, 742);
        if (couponBaseRespVo != null) return couponBaseRespVo;
        return BaseRespVo.ok();
    }

    private BaseRespVo getCouponBaseRespVo(int resultCode, int errno) {
        switch (resultCode) {
            case 10000:
                return BaseRespVo.error(errno, "该优惠券已删除");
            case 2000:
                return BaseRespVo.error(errno, "该优惠券已过期");
            case 300:
                return BaseRespVo.error(errno, "该优惠券已下架");
            case 40:
                return BaseRespVo.error(errno, "领取数量达上限");
            case 5:
                return BaseRespVo.error(errno, "优惠券数量不足");
            case -10000:
                return BaseRespVo.error(errno, "不支持领取方式");
        }
        return null;
    }

    /**
     * 个人主页优惠券选项显示优惠券
     * ?status=1&page=1&limit=10
     * 需要VO手摇车
     * @return CommonData
     */
    @RequestMapping("mylist")
    public BaseRespVo mylist(Short status, BasePageInfo basePageInfo) {
        // 判断登录
        BaseRespVo loginBaseRespVo = getLoginBaseRespVo();
        if (loginBaseRespVo != null) return loginBaseRespVo;

        CommonData<CouponUserVO> data = wxCouponService.mylist(status, basePageInfo);
        return BaseRespVo.ok(data);
    }

    /**
     * cartId为0时需要靠userId, checked=1, deleted=0来确定商品
     * 需要手摇车, 可以把所有可用的优惠券都查出来，再根据需求设置available，limit和total相同
     * @param cartId
     */
    @RequestMapping("selectlist")
    public BaseRespVo selectlist(Integer cartId, Integer grouponId) {
        // 判断登录
        BaseRespVo loginBaseRespVo = getLoginBaseRespVo();
        if (loginBaseRespVo != null) return loginBaseRespVo;

        CommonData<CouponUserVO> data = wxCouponService.selectlist(cartId);
        return BaseRespVo.ok(data);
    }
}
